﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using MySql.Data.MySqlClient;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace TravellerApp1
{
    /// <summary>
    /// QueryWindow.xaml 的交互逻辑
    /// </summary>
    public partial class QueryWindow : Window
    {
        public QueryWindow()
        {
            InitializeComponent();
            table_type.ItemsSource = TableTypeWrappers.All;
            table_type.SelectedValuePath = "TableType";
            table_type.DisplayMemberPath = "Description";
        }

        public enum TableType
        {
            FLIGHTS,
            HOTELS,
            BUS,
            CUSTOMERS
        }

        private Dictionary<string, string> PrimaryKeyDictionary = new()
        {
            { "FLIGHTS", "flightNum" },
            { "HOTELS", "location" },
            { "BUS", "location" },
            { "CUSTOMERS", "custName" }
        };

        public class TableTypeWrapper
        {
            public TableType TableType { get; }
            public string? Description { get; }
            public TableTypeWrapper(TableType type, string description)
            {
                this.TableType = type;
                this.Description = description;
            }
        }

        public static class TableTypeWrappers
        {
            public static IEnumerable<TableTypeWrapper> All =>
                new List<TableTypeWrapper>
                {
                    new TableTypeWrapper(TableType.FLIGHTS, "航班"),
                    new TableTypeWrapper(TableType.HOTELS, "宾馆房间"),
                    new TableTypeWrapper(TableType.BUS, "大巴车"),
                    new TableTypeWrapper(TableType.CUSTOMERS, "客户")
                };
        }

        public class Flight
        {
            public string flightNum { get; set; }
            public int price { get; set; }
            public int numSeats { get; set; }
            public int numAvail { get; set; }
            public string FromCity { get; set; }
            public string ArivCity { get; set; }
        }

        ObservableCollection<Flight> flightList = new();

        private void btn_query_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string cmd1 = "select * from " + table_type.SelectedValue.ToString();
                if(tb_key.Text != String.Empty)
                {
                    cmd1 += " where " + PrimaryKeyDictionary[table_type.SelectedValue.ToString()] + "='" + tb_key.Text + "'";
                }
                cmd1 += ";";
                MySqlDataReader reader = MysqlHelper.ExecuteSqlScript(cmd1);
                if(table_type.Text == "航班")
                {
                    flightList.Clear();
                    while (reader.Read())
                    {
                        flightList.Add(new Flight()
                        {
                            flightNum = reader[0].ToString(),
                            price = int.Parse(reader[1].ToString()),
                            numSeats = int.Parse(reader[2].ToString()),
                            numAvail = int.Parse(reader[3].ToString()),
                            FromCity = reader[4].ToString(),
                            ArivCity = reader[5].ToString()
                        });
                    }
                    MysqlHelper.CloseCon();
                    dg.ItemsSource = flightList;
                }
                //else if(...)
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MysqlHelper.SetDatabase("travel947");
            }
            finally
            {
                MysqlHelper.CloseCon();
            }
        }
    }
}
